using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKASTKE
    {
        public DataRow Convert2KASTKE(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKASTKE(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KKEMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KKEKANR"] = " ".PadRight(8 ); 
         dwReturn["KKEKAPOS"] = " ".PadRight(4 ); 
         dwReturn["KKEPOS"] = " ".PadRight(2 ); 
         dwReturn["KKEKART"] = " ".PadRight(20 ); 
         dwReturn["KKEKVAR"] = " ".PadRight(2 ); 
         dwReturn["KKEKFARBE"] = " ".PadRight(10 ); 
         dwReturn["KKEBAUMKZ"] = " ".PadRight(1 ); 
         dwReturn["KKEABF"] = " ".PadRight(0 ); 
         dwReturn["KKENET"] = " ".PadRight(0 ); 
         dwReturn["KKEBRUT"] = " ".PadRight(0 ); 
         dwReturn["KKEPREIS"] = " ".PadRight(0 ); 
         dwReturn["KKEKOSTEN"] = " ".PadRight(0 ); 
         dwReturn["KKEMGK1"] = " ".PadRight(0 ); 
         dwReturn["KKEMGK2"] = " ".PadRight(0 ); 
         dwReturn["KKEMGK3"] = " ".PadRight(0 ); 
         dwReturn["KKEMGK4"] = " ".PadRight(0 ); 
         dwReturn["KKEMGK5"] = " ".PadRight(0 ); 
         dwReturn["KKEKEAEND"] = " ".PadRight(1 ); 
         dwReturn["KKENETAEND"] = " ".PadRight(1 ); 
         dwReturn["KKEABFAEND"] = " ".PadRight(1 ); 
         dwReturn["KKEPRAEND"] = " ".PadRight(1 ); 
         dwReturn["KKEEAFAKTR"] = " ".PadRight(0 ); 
         dwReturn["KKELLEIST"] = " ".PadRight(4 ); 
         dwReturn["KKERLEIST"] = " ".PadRight(4 ); 
         dwReturn["KKEANZBA"] = " ".PadRight(2 ); 
         dwReturn["KKEDIVISOR"] = " ".PadRight(2 ); 
         dwReturn["KKEFILLER"] = " ".PadRight(93 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKASTKE(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKASTKE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKASTKE(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKASTKE(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKASTKE(dwLocal, ITXDBVERSION);
            }
            UpdateKASTKE(dwLocal);
        }
        public void InsertKASTKE(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KASTKE(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKASTKE(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KASTKE WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KASTKE  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KASTKE";
            Return = Result;
            return Return;
        }
        public void DeleteKASTKE(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KASTKE   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKASTKE(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KASTKE SET "
                    +"KKEMANDANT= :KKEMANDANT,"
                    +"KKEKANR= :KKEKANR,"
                    +"KKEKAPOS= :KKEKAPOS,"
                    +"KKEPOS= :KKEPOS,"
                    +"KKEKART= :KKEKART,"
                    +"KKEKVAR= :KKEKVAR,"
                    +"KKEKFARBE= :KKEKFARBE,"
                    +"KKEBAUMKZ= :KKEBAUMKZ,"
                    +"KKEABF= :KKEABF,"
                    +"KKENET= :KKENET,"
                    +"KKEBRUT= :KKEBRUT,"
                    +"KKEPREIS= :KKEPREIS,"
                    +"KKEKOSTEN= :KKEKOSTEN,"
                    +"KKEMGK1= :KKEMGK1,"
                    +"KKEMGK2= :KKEMGK2,"
                    +"KKEMGK3= :KKEMGK3,"
                    +"KKEMGK4= :KKEMGK4,"
                    +"KKEMGK5= :KKEMGK5,"
                    +"KKEKEAEND= :KKEKEAEND,"
                    +"KKENETAEND= :KKENETAEND,"
                    +"KKEABFAEND= :KKEABFAEND,"
                    +"KKEPRAEND= :KKEPRAEND,"
                    +"KKEEAFAKTR= :KKEEAFAKTR,"
                    +"KKELLEIST= :KKELLEIST,"
                    +"KKERLEIST= :KKERLEIST,"
                    +"KKEANZBA= :KKEANZBA,"
                    +"KKEDIVISOR= :KKEDIVISOR,"
                    +"KKEFILLER= :KKEFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KKEMANDANT", OracleType.NVarChar).Value = dwData["KKEMANDANT"].ToString(); 
          or1.Parameters.Add("KKEKANR", OracleType.NVarChar).Value = dwData["KKEKANR"].ToString(); 
          or1.Parameters.Add("KKEKAPOS", OracleType.NVarChar).Value = dwData["KKEKAPOS"].ToString(); 
          or1.Parameters.Add("KKEPOS", OracleType.NVarChar).Value = dwData["KKEPOS"].ToString(); 
          or1.Parameters.Add("KKEKART", OracleType.NVarChar).Value = dwData["KKEKART"].ToString(); 
          or1.Parameters.Add("KKEKVAR", OracleType.NVarChar).Value = dwData["KKEKVAR"].ToString(); 
          or1.Parameters.Add("KKEKFARBE", OracleType.NVarChar).Value = dwData["KKEKFARBE"].ToString(); 
          or1.Parameters.Add("KKEBAUMKZ", OracleType.NVarChar).Value = dwData["KKEBAUMKZ"].ToString(); 
          or1.Parameters.Add("KKEABF", OracleType.NVarChar).Value = dwData["KKEABF"].ToString(); 
          or1.Parameters.Add("KKENET", OracleType.NVarChar).Value = dwData["KKENET"].ToString(); 
          or1.Parameters.Add("KKEBRUT", OracleType.NVarChar).Value = dwData["KKEBRUT"].ToString(); 
          or1.Parameters.Add("KKEPREIS", OracleType.NVarChar).Value = dwData["KKEPREIS"].ToString(); 
          or1.Parameters.Add("KKEKOSTEN", OracleType.NVarChar).Value = dwData["KKEKOSTEN"].ToString(); 
          or1.Parameters.Add("KKEMGK1", OracleType.NVarChar).Value = dwData["KKEMGK1"].ToString(); 
          or1.Parameters.Add("KKEMGK2", OracleType.NVarChar).Value = dwData["KKEMGK2"].ToString(); 
          or1.Parameters.Add("KKEMGK3", OracleType.NVarChar).Value = dwData["KKEMGK3"].ToString(); 
          or1.Parameters.Add("KKEMGK4", OracleType.NVarChar).Value = dwData["KKEMGK4"].ToString(); 
          or1.Parameters.Add("KKEMGK5", OracleType.NVarChar).Value = dwData["KKEMGK5"].ToString(); 
          or1.Parameters.Add("KKEKEAEND", OracleType.NVarChar).Value = dwData["KKEKEAEND"].ToString(); 
          or1.Parameters.Add("KKENETAEND", OracleType.NVarChar).Value = dwData["KKENETAEND"].ToString(); 
          or1.Parameters.Add("KKEABFAEND", OracleType.NVarChar).Value = dwData["KKEABFAEND"].ToString(); 
          or1.Parameters.Add("KKEPRAEND", OracleType.NVarChar).Value = dwData["KKEPRAEND"].ToString(); 
          or1.Parameters.Add("KKEEAFAKTR", OracleType.NVarChar).Value = dwData["KKEEAFAKTR"].ToString(); 
          or1.Parameters.Add("KKELLEIST", OracleType.NVarChar).Value = dwData["KKELLEIST"].ToString(); 
          or1.Parameters.Add("KKERLEIST", OracleType.NVarChar).Value = dwData["KKERLEIST"].ToString(); 
          or1.Parameters.Add("KKEANZBA", OracleType.NVarChar).Value = dwData["KKEANZBA"].ToString(); 
          or1.Parameters.Add("KKEDIVISOR", OracleType.NVarChar).Value = dwData["KKEDIVISOR"].ToString(); 
          or1.Parameters.Add("KKEFILLER", OracleType.NVarChar).Value = dwData["KKEFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
